package myky.com.doctorapp.ui

import android.app.Activity
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import com.gyf.barlibrary.ImmersionBar
import kotlinx.android.synthetic.main.activity_web_protro.*
import myky.com.doctorapp.R
import myky.com.doctorapp.interface_.JsApi
import myky.com.doctorapp.utils.Constants

class WebProtocolActivity : Activity() {
     var url: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_web_protro)
        ImmersionBar.with(this).barColorInt(resources.getColor(R.color.colorPrimary)).titleBar(R.id.my_self_title).navigationBarEnable(false).init()



        val intent = intent.extras
        url = intent.getString(Constants.WEBURL)
        val tv_title = intent.getString(Constants.TITLE_H5)
        if (tv_title != null) {
            my_title_tv1.text = tv_title
        }
        web_doctor_pb.max = 100
        act_banner_webview.isHorizontalScrollBarEnabled = false//水平不显示
        act_banner_webview.isVerticalScrollBarEnabled = false //垂直不显示
        act_banner_webview.settings.setSupportZoom(true)
        act_banner_webview.settings.builtInZoomControls = true
        act_banner_webview.addJavascriptObject(JsApi(), null)
        act_banner_webview.webChromeClient = WebProgressClient()
        act_banner_webview.webViewClient = object : WebViewClient() {}
        act_banner_webview.loadUrl(url)
        backLayout.setOnClickListener { finish() }
    }

    //加载进度回调
    private inner class WebProgressClient : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            if (web_doctor_pb != null) {
                web_doctor_pb.progress = newProgress
                if (newProgress == 100) {
                    web_doctor_pb.visibility = View.GONE
                }
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        if (keyCode == KeyEvent.KEYCODE_BACK && act_banner_webview.canGoBack()) {
            act_banner_webview.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }


}
